const express = require('express')
const router = express.Router()
const tagsAction = require('../Controller/tag')



//tags
router.get('/tags-admin', function (req, res) {
  tagsAction.all( function(tags){
      console.log(tags)
      res.render('tags-admin.html',{tags})
    }
  )
})

router.post('/tags-edit:id', function (req, res) {
  let session =req.session.user
  if(!session){
    return res.render('jump.html',{msg:'登录信息失效，请重新登录'})
  }
  if(session.level==3){ //只有管理员有权限
    return res.render('noAuthority.html')
  }
  let id =req.params.id
  tagsAction.updateById(id,function(tags){
      res.redirect('/admin/tags-admin')
    }
  )
})

router.post('/tagAdd',function(req,res){
  let session =req.session.user
  if(!session){
    return res.render('jump.html',{msg:'登录信息失效，请重新登录'})
  }
  if(session.level==3){ //只有管理员有权限
    return res.render('noAuthority.html')
  }
  let newtag = req.body
  console.log('接受',newtag)
  tagsAction.add(newtag,function(result){
    res.redirect('/admin/tags-admin')
  })

})

module.exports = router